/*! ******************************************************************************
 *
 * Created on 2022-08-01
 *
 * Copyright(c) YuZhou Big Data Laboratory CO.,Ltd, 2022.  All rights reserved.
 *
 *******************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************************/

package org.yzbdl.lanius.ui.plugin.dialog;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
import org.pentaho.di.core.EngineMetaInterface;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.exception.KettleFileException;
import org.pentaho.di.core.exception.KettleMissingPluginsException;
import org.pentaho.di.core.exception.KettleXMLException;
import org.pentaho.di.ui.core.enums.PluginType;
import org.pentaho.di.ui.core.gui.LaniusGuiResource;
import org.pentaho.di.ui.core.gui.GUIResource;
import org.pentaho.di.ui.spoon.ChangedWarningInterface;
import org.pentaho.di.ui.spoon.Spoon;
import org.pentaho.di.ui.spoon.TabItemInterface;
import org.yzbdl.lanius.core.dialog.PanelUtils;
import org.yzbdl.lanius.core.exception.LaniusEngineDbException;
import org.yzbdl.lanius.core.plugins.metastore.LdrPluginDao;
import org.yzbdl.lanius.core.plugins.metastore.LdrPluginEntity;
import org.yzbdl.lanius.core.plugins.service.PluginService;
import org.yzbdl.lanius.core.plugins.unInstall.PluginUninstaller;
import org.yzbdl.lanius.ui.plugindto.LocalPluginDto;
import org.yzbdl.lanius.util.LogUtil;

import com.google.common.collect.Lists;

import cn.hutool.core.util.ObjectUtil;

/**
 * 插件管理面板
 *
 * @author tanpeng@yzbdl.ac.cn
 * @version 1.0
 * @date 2022-08-01 11:19
 */
public class PluginListDelegate extends Composite implements TabItemInterface {

    private static final int STYLE = SWT.DIALOG_TRIM | SWT.RESIZE;
    private static final String NONE = "none";
    private final Font titleFont = new Font(null, "微软雅黑", 15, SWT.BOLD);
    private final Font contentFont = new Font(null, "微软雅黑", 10, SWT.NORMAL);

    private final ScrolledComposite scrolledComposite;
    private final Composite pluginListComp;
    private Composite pluginGroup;

    /** 搜索内容 */
    private String searchTypeCode = "";
    private Text searchKey;

    /** 分页 */
    private Label sumPage, nowPage;

    public final static String SEARCH = "搜索";
    public final static String PREVIOUS_PAGE = "上一页", NEXT_PAGE = "下一页";
    private Integer currentPage = 1;
    private Integer totalPages;
    private Integer offset = 0;
    public final Integer PAGE_SIZE = 10;

    private final PluginService pluginService = PluginService.getInstance();
    private final LdrPluginDao ldrPluginDao = LdrPluginDao.getInstance();

    /**
     * Create the composite.
     */
    public PluginListDelegate(Composite parent) {
        super(parent, STYLE);
        setBackground(GUIResource.getInstance().getColorWhite());
        setLayout(new FillLayout());

        scrolledComposite = new ScrolledComposite(this, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
        scrolledComposite.setLayout(new FillLayout());
        scrolledComposite.setBackground(GUIResource.getInstance().getColorWhite());
        scrolledComposite.setExpandHorizontal(true);
        scrolledComposite.setExpandVertical(true);

        pluginListComp = new Composite(scrolledComposite, SWT.NONE);
        pluginListComp.setBackground(GUIResource.getInstance().getColorWhite());
        pluginListComp.setLayout(new GridLayout());
        scrolledComposite.setContent(pluginListComp);

        // 搜索工具
        drawSearchTool();

        List<LocalPluginDto.DataBean> dataBeans = getData("", "");
        totalPages = pluginService.getAllPlugin("", "") / PAGE_SIZE;
        if (pluginService.getAllPlugin("", "") % PAGE_SIZE != 0) {
            totalPages += 1;
        }
        // 插件列表
        drawPluginList(dataBeans);
    }

    /**
     * 获取后台数据
     * @param pluginName 插件名字
     * @param pluginTypeCodeStr 插件类型
     * @return 插件列表
     */
    public List<LocalPluginDto.DataBean> getData(String pluginName, String pluginTypeCodeStr) {
        totalPages = pluginService.getAllPlugin(pluginName, pluginTypeCodeStr) / PAGE_SIZE;
        if (pluginService.getAllPlugin(pluginName, pluginTypeCodeStr) % PAGE_SIZE != 0) {
            totalPages += 1;
        }
        // 查询本地插件
        Set<String> mapList = pluginService.getPageList(PAGE_SIZE, offset, pluginName, pluginTypeCodeStr);
        if (mapList.isEmpty()) {
            return null;
        }

        List<LocalPluginDto.DataBean> list = Lists.newArrayList();
        getLocalPlugin(mapList, list);
        return list;
    }

    /**
     * 获取本地数据库的插件
     * @param mapList 插件ID列表
     * @param list 插件列表
     */
    private void getLocalPlugin(Set<String> mapList, List<LocalPluginDto.DataBean> list) {
        for (String pluginId : mapList) {
            LocalPluginDto.DataBean dataBean = new LocalPluginDto.DataBean();
            LdrPluginEntity ldrPluginEntity = null;
            try {
                ldrPluginEntity = pluginService.findByPluginId(pluginId);
            } catch (LaniusEngineDbException ex) {
                LogUtil.error("数据库查询失败");
            }
            assert ldrPluginEntity != null;
            dataBean.setCreateTime(ldrPluginEntity.getCreateTime());
            dataBean.setPlugin_type_code_str(ldrPluginEntity.getAnnotationTypeShortName());
            dataBean.setAvg_score("0.0");
            dataBean.setDownload_number("0");
            dataBean.setDescription(ldrPluginEntity.getProfile());
            dataBean.setIcon_path(ldrPluginEntity.getImage());
            dataBean.setPlugin_meta_id(ldrPluginEntity.getPluginId());
            dataBean.setPlugin_name(ldrPluginEntity.getName());
            dataBean.setReal_name("未知");
            dataBean.setPluginTypeClass(ldrPluginEntity.getPluginTypeName());
            dataBean.setPlugin_size("0");
            list.add(dataBean);
        }
    }

    /**
     * 插件列表
     */
    private void drawPluginList(List<LocalPluginDto.DataBean> pluginData) {
        scrolledComposite.setMinSize(1200, 2360);
        pluginGroup = new Composite(pluginListComp, SWT.NONE);
        pluginGroup.setBackground(GUIResource.getInstance().getColorWhite());
        GridData gdPluginGroup = new GridData(GridData.FILL_HORIZONTAL);
        gdPluginGroup.horizontalAlignment = SWT.CENTER;
        pluginGroup.setLayoutData(gdPluginGroup);
        pluginGroup.setLayout(new GridLayout());
        if (ObjectUtil.isEmpty(pluginData)) {
            scrolledComposite.setMinSize(1200, 600);
            Composite emptyComp = new Composite(pluginGroup, SWT.NONE);
            emptyComp.setBackground(GUIResource.getInstance().getColorWhite());
            Label noData = new Label(pluginGroup, SWT.NONE);
            noData.setBackground(GUIResource.getInstance().getColorWhite());
            noData.setBounds(0, 240, 128, 128);
            noData.setText("暂无数据");
            pluginListComp.layout();
            pluginGroup.layout();
            return;
        }
        for (LocalPluginDto.DataBean contentBean : pluginData) {
            drawPluginInfo(contentBean);
        }
        // 分页
        drawPageTool();
        sumPage.setText("总计" + totalPages + "页");
        if (totalPages == 0) {
            nowPage.setText("当前第1页");
        } else {
            nowPage.setText("当前第" + currentPage + "页");
        }
        pluginListComp.layout();
        pluginGroup.layout();
    }

    /**
     * 空处理
     *
     */
    private String notNull(String source) {
        return Optional.ofNullable(source).orElse("");
    }

    /**
     * 获取文本宽度
     */
    private int getTextWith(String words, Font font) {
        Label text = new Label(this, SWT.NONE);
        text.setText(words);
        text.setFont(font);
        int width = text.computeSize(SWT.DEFAULT, SWT.DEFAULT).x;
        text.dispose();
        return width;
    }

    /**
     * 绘制插件信息
     */
    private void drawPluginInfo(LocalPluginDto.DataBean contentBean) {
        // 列表内容
        Composite pluginComp = new Composite(pluginGroup, SWT.NONE);
        pluginComp.setBackground(GUIResource.getInstance().getColorWhite());

        Label pluginImage = new Label(pluginComp,  SWT.SHADOW_IN | SWT.CENTER);
        pluginImage.setBackground(GUIResource.getInstance().getColorWhite());
        pluginImage.setBounds(0, 20, 136, 136);

        Label pluginName = new Label(pluginComp, SWT.NONE);
        pluginName.setBackground(GUIResource.getInstance().getColorWhite());
        pluginName.setFont(titleFont);
        int pluginNameWidth = getTextWith(notNull(contentBean.getPlugin_name()), titleFont);
        pluginName.setBounds(150, 20, pluginNameWidth, 36);
        pluginName.setText(contentBean.getPlugin_name());

        Label pluginId = new Label(pluginComp, SWT.NONE);
        pluginId.setText(notNull(contentBean.getPlugin_meta_id()));
        pluginId.setText(contentBean.getPlugin_meta_id());
        pluginId.setBackground(GUIResource.getInstance().getColorWhite());
        pluginId.setBounds(180 + pluginNameWidth, 36, 320, 25);

        Label pluginCategory = new Label(pluginComp, SWT.CENTER);
        pluginCategory.setBackground(GUIResource.getInstance().getColor(233, 233, 188));
        String type = PluginType.getByCode(contentBean.getPlugin_type_code_str()) != null
            ? Objects.requireNonNull(PluginType.getByCode(contentBean.getPlugin_type_code_str())).getPluginName()
            : "未知";
        pluginCategory.setText(type);
        int typeWidth = getTextWith(type, contentFont);
        pluginCategory.setBounds(540, 72, typeWidth + 10, 24);

        SimpleDateFormat dateTimeFormatter = new SimpleDateFormat("yyyy-MM-dd");
        Label pluginInstallDate = new Label(pluginComp, SWT.NONE);
        pluginInstallDate.setBackground(GUIResource.getInstance().getColorWhite());
        pluginInstallDate.setText("安装日期：" + dateTimeFormatter.format(contentBean.getCreateTime()));
        pluginInstallDate.setBounds(660, 72, 150, 30);

        Label pluginSize = new Label(pluginComp, SWT.NONE);
        pluginSize.setBackground(GUIResource.getInstance().getColorWhite());
        pluginSize.setBounds(840, 72, 100, 30);
        pluginSize.setText("大小：约1MB");

        Label pluginVersion = new Label(pluginComp, SWT.NONE);
        pluginVersion.setBackground(GUIResource.getInstance().getColorWhite());
        pluginVersion.setBounds(950, 72, 50, 30);
        pluginVersion.setText("版本：");

        Combo pluginVersionList = new Combo(pluginComp, SWT.BORDER);
        pluginVersionList.setBounds(1000, 72, 90, 30);
        List<LdrPluginEntity> ldrPluginEntityList = pluginService.getPluginVersions(contentBean.getPlugin_meta_id());
        for (LdrPluginEntity version : ldrPluginEntityList) {
            pluginVersionList.add(version.getVersion());
        }
        pluginVersionList.select(0);

        // 先渲染版本列表再渲染图片
        Image image = getImage(contentBean, pluginVersionList);
        if (image == null) {
            try {
                // 是否不能使用SVG
                image = GUIResource.getInstance().getImage("ui/images/RPL.svg", 128, 128);
//                image = LaniusGuiResource.getInstance().getCpImage("ui/images/RPL.png", 96, 96);
                ImageData imageData = image.getImageData().scaledTo(128, 128);
                image = new Image(this.getDisplay(), imageData);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        pluginImage.setImage(image);

        Label pluginDesc = new Label(pluginComp, SWT.NONE);
        pluginDesc.setBackground(GUIResource.getInstance().getColorWhite());
        pluginDesc.setBounds(150, 130, 1050, 48);
        pluginDesc.setText(contentBean.getDescription());

        Button uninstallButton = new Button(pluginComp, SWT.NONE);
        uninstallButton.setText("卸载");
        uninstallButton.setBounds(1100, 64, 100, 42);
        addUninstallListener(contentBean, pluginVersionList, uninstallButton);

        Label pluginSplitLine = new Label(pluginComp, SWT.SEPARATOR | SWT.HORIZONTAL);
        pluginSplitLine.setLocation(0, 180);
        pluginSplitLine.setSize(1200, 1);
    }

    /**
     * 绑定卸载事件
     * @param contentBean 对象
     * @param pluginVersionList 版本列表
     * @param uninstallButton 卸载按钮
     */
    private void addUninstallListener(LocalPluginDto.DataBean contentBean, Combo pluginVersionList,
        Button uninstallButton) {
        uninstallButton.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent selectionEvent) {
                try {
                    MessageBox box = new MessageBox(getParent().getShell(),
                        SWT.ICON_WARNING | SWT.APPLICATION_MODAL | SWT.SHEET | SWT.YES | SWT.NO);
                    box.setText("警告");
                    box.setMessage("您确定要卸载该插件吗？");
                    int answer = box.open();
                    if (answer != SWT.YES) {
                        return;
                    }
                    TabItemInterface activeTabItem = Spoon.getInstance().getActiveTabitem();
                    if (activeTabItem != null) {
                        if (activeTabItem.hasContentChanged()) {
                            MessageBox mb = new MessageBox(getParent().getShell(), SWT.OK | SWT.ICON_WARNING);
                            mb.setMessage("您有未保存的转换,请保存后再进行卸载操作");
                            mb.setText("提示");
                            mb.open();
                            return;
                        }
                    }
                    LdrPluginEntity entity =
                        ldrPluginDao.getLocalVersionOne(contentBean.getPlugin_meta_id(), pluginVersionList.getText());
                    if (entity == null) {
                        MessageBox mb = new MessageBox(getParent().getShell(), SWT.OK | SWT.ICON_INFORMATION);
                        mb.setMessage("该插件已被卸载,！");
                        showMessage(activeTabItem, mb);
                        return;
                    }
                    Boolean isSuccess = PluginUninstaller.getInstance().uninstallPlugin(entity.getPluginId(),
                        entity.getPluginTypeName(), entity.getVersion());
                    MessageBox mb = new MessageBox(getParent().getShell(), SWT.OK | SWT.ICON_INFORMATION);
                    if (isSuccess) {
                        mb.setMessage("卸载插件成功！");
                        showMessage(activeTabItem, mb);
                    } else {
                        mb.setMessage("卸载插件失败！");
                        mb.setText("提示");
                        mb.open();
                    }
                } catch (LaniusEngineDbException e) {
                    e.printStackTrace();
                } catch (KettleException e) {
                    MessageBox mb = new MessageBox(getParent().getShell(), SWT.OK | SWT.ICON_WARNING);
                    mb.setMessage("加载转换失败！");
                    mb.setText("提示");
                    mb.open();
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * 显示消息
     * @param activeTabItem item
     * @param mb 消息框
     * @throws KettleXMLException  异常1
     * @throws KettleMissingPluginsException  异常2
     */
    private void showMessage(TabItemInterface activeTabItem, MessageBox mb)
        throws KettleXMLException, KettleMissingPluginsException {
        mb.setText("提示");
        mb.open();
        PanelUtils.updatePanel(activeTabItem);
        pluginGroup.dispose();
        List<LocalPluginDto.DataBean> data = getData(searchKey.getText(), getPluginTypeCode());
        Spoon.getInstance().refreshGraph();
        drawPluginList(data);
        pluginListComp.layout();
        pluginGroup.layout();
    }

    /**
     * 获取pluginCode
     */
    public String getPluginTypeCode() {
        return searchTypeCode;
    }

    /**
     * 获取图像
     * @param contentBean 实体对象
     * @param pluginVersionListComp 版本列表
     * @return 图片
     */
    private Image getImage(LocalPluginDto.DataBean contentBean, Combo pluginVersionListComp) {
        Image image = null;
        if (NONE.equals(pluginVersionListComp.getText())) {
            LdrPluginEntity ldrPluginEntity = null;
            try {
                ldrPluginEntity = ldrPluginDao.getLocalVersionOne(contentBean.getPlugin_meta_id(), NONE);
            } catch (LaniusEngineDbException e) {
                e.printStackTrace();
            }
            assert ldrPluginEntity != null;
            image = GUIResource.getInstance().getImage(ldrPluginEntity.getImage(),
                ldrPluginEntity.getMainClass().getClassLoader(), 70, 70);
        } else {
            LdrPluginEntity ldrPluginEntity = null;
            try {
                ldrPluginEntity =
                    ldrPluginDao.getLocalVersionOne(contentBean.getPlugin_meta_id(), pluginVersionListComp.getText());
            } catch (LaniusEngineDbException e) {
                e.printStackTrace();
            }
            try {
                assert ldrPluginEntity != null;
                image = LaniusGuiResource.getInstance().getPluginImage(ldrPluginEntity.getFolderName(),
                    pluginVersionListComp.getText(), 128);
                ImageData imageData = image.getImageData().scaledTo(128, 128);
                image = new Image(this.getDisplay(), imageData);
            } catch (KettleFileException e) {
                //
            }
        }
        return image;
    }

    /**
     * 创建搜索工具
     */
    private void drawSearchTool() {
        Composite searchTool = new Composite(pluginListComp, SWT.NONE);
        searchTool.setBackground(GUIResource.getInstance().getColorWhite());
        configurationCompose(searchTool, 50, 30, 50, 20);

        Composite textComp = new Composite(searchTool, SWT.BORDER);
        textComp.setBackground(GUIResource.getInstance().getColorWhite());
        RowLayout rlTextComp = new RowLayout(SWT.VERTICAL);
        rlTextComp.marginWidth = 18;
        rlTextComp.marginTop = 18;
        textComp.setLayout(rlTextComp);

        searchKey = new Text(textComp, SWT.NONE);
        searchKey.setFont(contentFont);
        searchKey.setLayoutData(new RowData(420, 36));
        searchKey.setMessage("插件名称/插件ID");

        Button searchButton = new Button(searchTool, SWT.NONE);
        searchButton.setLayoutData(new RowData(120, 36));
        searchButton.setText("搜索");
        searchButton.addSelectionListener(new EventAdapter());

        Composite typeBtnTool = new Composite(pluginListComp, SWT.NONE);
        configurationCompose(typeBtnTool, 15, 15, 45, 32);

        addButton(typeBtnTool);

        // 分割线
        Composite lineComp = new Composite(pluginListComp, SWT.NONE);
        GridData gdLineComp = new GridData(GridData.FILL_HORIZONTAL);
        gdLineComp.horizontalAlignment = SWT.CENTER;
        lineComp.setLayoutData(gdLineComp);
        lineComp.setBackground(GUIResource.getInstance().getColorWhite());
        RowLayout lineCompLayout = new RowLayout();
        lineCompLayout.marginTop = 30;
        lineComp.setLayout(lineCompLayout);

        Label splitLine = new Label(lineComp, SWT.SEPARATOR | SWT.HORIZONTAL);
        splitLine.setLayoutData(new RowData(1200, 1));
        splitLine.setBackground(GUIResource.getInstance().getColorWhite());
    }

    /**
     * compose 公共配置
     * @param composite 面板
     * @param marginTop 上边距
     * @param marginBottom 下边距
     * @param marginWidth 左右边距
     * @param spacing 组件边距
     */
    private void configurationCompose(Composite composite, int marginTop, int marginBottom, int marginWidth,
        int spacing) {
        GridData gdComposite = new GridData(GridData.FILL_HORIZONTAL);
        gdComposite.horizontalAlignment = SWT.CENTER;
        composite.setLayoutData(gdComposite);
        RowLayout compositeLayout = new RowLayout();
        compositeLayout.fill = true;
        compositeLayout.center = true;
        compositeLayout.marginTop = marginTop;
        compositeLayout.marginBottom = marginBottom;
        compositeLayout.marginWidth = marginWidth;
        compositeLayout.spacing = spacing;
        composite.setLayout(compositeLayout);
    }

    /**
     * 添加按钮
     * @param typeBtnTool 按钮所在面板
     */
    private void addButton(Composite typeBtnTool) {
        Button btnAll = new Button(typeBtnTool, SWT.FLAT);
        btnAll.setLayoutData(new RowData(90, 42));
        btnAll.setText("全部");
        btnAll.setData("");
        btnAll.addSelectionListener(new SearchTypeAdapter());

        Button btnTrans = new Button(typeBtnTool, SWT.FLAT);
        btnTrans.setLayoutData(new RowData(135, 42));
        btnTrans.setText("转换插件");
        btnTrans.setData("Step");
        btnTrans.addSelectionListener(new SearchTypeAdapter());

        Button btnJob = new Button(typeBtnTool, SWT.FLAT);
        btnJob.setLayoutData(new RowData(135, 42));
        btnJob.setText("作业插件");
        btnJob.setData("JobEntry");
        btnJob.addSelectionListener(new SearchTypeAdapter());

        Button btnInterface = new Button(typeBtnTool, SWT.FLAT);
        btnInterface.setLayoutData(new RowData(135, 42));
        btnInterface.setText("接口插件");
        btnInterface.setData("CarteServlet");
        btnInterface.addSelectionListener(new SearchTypeAdapter());

        Button btnDb = new Button(typeBtnTool, SWT.FLAT);
        btnDb.setLayoutData(new RowData(150, 42));
        btnDb.setText("数据库插件");
        btnDb.setData("DatabaseMetaPlugin");
        btnDb.addSelectionListener(new SearchTypeAdapter());

        Button btnRep = new Button(typeBtnTool, SWT.FLAT);
        btnRep.setLayoutData(new RowData(150, 42));
        btnRep.setText("资源库插件");
        btnRep.setData("RepositoryPlugin");
        btnRep.addSelectionListener(new SearchTypeAdapter());

        Button btnCompress = new Button(typeBtnTool, SWT.FLAT);
        btnCompress.setLayoutData(new RowData(135, 42));
        btnCompress.setText("压缩插件");
        btnCompress.setData("CompressionPlugin");
        btnCompress.addSelectionListener(new SearchTypeAdapter());
    }

    /**
     * 触发翻页
     */
    public void select(SelectionEvent selectionEvent) {
        if (selectionEvent != null) {
            String text = ((Button)selectionEvent.widget).getText();
            if (PREVIOUS_PAGE.equals(text)) {
                if (currentPage > 1) {
                    currentPage -= 1;
                }
            } else if (NEXT_PAGE.equals(text)) {
                if (currentPage < totalPages) {
                    currentPage += 1;
                }
            } else if (SEARCH.equals(text)) {
                currentPage = 1;
            } else {
                currentPage = 1;
            }
            // 查询本地插件
            if (currentPage == 1) {
                offset = 0;
            } else {
                offset = (currentPage - 1) * PAGE_SIZE;
            }
        }
        if (pluginGroup != null) {
            pluginGroup.dispose();
        }
        List<LocalPluginDto.DataBean> data = getData(searchKey.getText(), getPluginTypeCode());
        drawPluginList(data);
    }

    /**
     * 事件适配器
     */
    private class EventAdapter extends SelectionAdapter {
        @Override
        public void widgetSelected(SelectionEvent selectionEvent) {
            select(selectionEvent);
            super.widgetSelected(selectionEvent);
        }
    }

    /**
     * 搜索组件按钮事件适配器
     */
    private class SearchTypeAdapter extends SelectionAdapter {
        @Override
        public void widgetSelected(SelectionEvent selectionEvent) {
            searchTypeCode = (String)selectionEvent.widget.getData();
            select(selectionEvent);
            super.widgetSelected(selectionEvent);
        }
    }

    /**
     * 底部分页工具
     */
    private void drawPageTool() {
        // 分页工具
        Composite toolComp = new Composite(pluginGroup, SWT.NONE);
        toolComp.setBackground(GUIResource.getInstance().getColorWhite());
        GridData gdToolComp = new GridData(GridData.FILL_HORIZONTAL);
        gdToolComp.horizontalAlignment = SWT.CENTER;
        toolComp.setLayoutData(gdToolComp);
        RowLayout toolCompLayout = new RowLayout();
        toolCompLayout.fill = true;
        toolCompLayout.center = true;
        toolCompLayout.marginTop = 15;
        toolCompLayout.marginBottom = 15;
        toolCompLayout.marginLeft = 820;
        toolCompLayout.spacing = 24;
        toolComp.setLayout(toolCompLayout);

        Button lastPageBtn = new Button(toolComp, SWT.NONE);
        lastPageBtn.setBackground(GUIResource.getInstance().getColorWhite());
        lastPageBtn.setLayoutData(new RowData(100, 24));
        lastPageBtn.setText(PREVIOUS_PAGE);
        lastPageBtn.addSelectionListener(new EventAdapter());

        Button nextPageBtn = new Button(toolComp, SWT.NONE);
        nextPageBtn.setBackground(GUIResource.getInstance().getColorWhite());
        nextPageBtn.setLayoutData(new RowData(100, 24));
        nextPageBtn.setText(NEXT_PAGE);
        nextPageBtn.addSelectionListener(new EventAdapter());

        Composite labelComp = new Composite(toolComp, SWT.NONE);
        labelComp.setBackground(GUIResource.getInstance().getColorWhite());
        RowLayout rlLabelComp = new RowLayout(SWT.VERTICAL);
        rlLabelComp.justify = true;
        labelComp.setLayout(rlLabelComp);

        nowPage = new Label(labelComp, SWT.CENTER);
        nowPage.setBackground(GUIResource.getInstance().getColorWhite());
        sumPage = new Label(labelComp, SWT.CENTER);
        sumPage.setBackground(GUIResource.getInstance().getColorWhite());
    }

    @Override
    protected void checkSubclass() {
        // Disable the check that prevents subclassing of SWT components
    }

    @Override
    public boolean canBeClosed() {
        return false;
    }

    @Override
    public boolean canHandleSave() {
        return false;
    }

    @Override
    public Object getManagedObject() {
        return null;
    }

    @Override
    public boolean hasContentChanged() {
        return false;
    }

    @Override
    public ChangedWarningInterface getChangedWarning() {
        return null;
    }

    @Override
    public int showChangedWarning() throws KettleException {
        return 0;
    }

    @Override
    public boolean applyChanges() throws KettleException {
        return false;
    }

    @Override
    public EngineMetaInterface getMeta() {
        return null;
    }

    @Override
    public void setControlStates() {

    }
}
